package com.jy.adscm.zltx.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.SjFile;
import com.jy.adscm.zltx.bean.SjFileDept;
import com.jy.adscm.zltx.service.SjFileDeptService;
import com.jy.adscm.zltx.service.SjFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-11-08
 */
@RestController
@RequestMapping("/adscm/zltx/sjFile")
public class SjFileController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    SjFileService sjFileService;
    @Autowired
    SjFileDeptService sjFileDeptService;
    @CacheEvict(value = "sjFileUserTree" ,allEntries=true)
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody SjFile sjFile, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        String treeId=request.getParameter("treeId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
        sjFile.setFileWriter(token.getUser().getId().toString());
        sjFile.setCreateTime(new Date());
        sjFile.setIsApproval("0");
        sjFile.setIsLocked(0);
        boolean b = sjFileService.insertSjFile(sjFile, treeId);
        List<Integer> fileUseScopes = sjFile.getFileUseScope();
        for (Integer fileUseScope : fileUseScopes) {
            SjFileDept sjFileDept=new SjFileDept();
            sjFileDept.setDeptId( fileUseScope );
            sjFileDept.setFileId(sjFile.getFileId());
            sjFileDeptService.insert(sjFileDept);
        }
        return new SimpleCoreResponse("200","新增成功",b);
    }
    @CacheEvict(value = "sjFileUserTree" ,allEntries=true)
      @RequestMapping("update")
    public  SimpleCoreResponse update(@RequestBody SjFile sjFile, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
        if ("1".equals(sjFile.getIsApproval())){
            sjFile.setFileApprover(token.getUser().getId().toString());
            sjFile.setApprovalTime(new Date());
        }
        boolean insert = sjFileService.updateSjFile(sjFile);
        Wrapper<SjFileDept> wrapper= new EntityWrapper<>();
        wrapper.eq("fileId",sjFile.getFileId());
        sjFileDeptService.delete(wrapper);
        List<Integer> fileUseScopes = sjFile.getFileUseScope();
        for (Integer fileUseScope : fileUseScopes) {
            SjFileDept sjFileDept=new SjFileDept();
            sjFileDept.setDeptId( fileUseScope );
            sjFileDept.setFileId(sjFile.getFileId());
            sjFileDeptService.insert(sjFileDept);
        }
        return  new SimpleCoreResponse("200","修改成功",null);
    }
    @RequestMapping("selectByFileId/{fileGs}/{fileType}")
   public SimpleCoreResponse selectByFileId(@PathVariable String fileGs,@PathVariable String fileType, HttpServletRequest request)throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
        List<SjFile> sjFiles = sjFileService.selectByFileId(fileGs,fileType,token);
        return new SimpleCoreResponse("200","查询成功",sjFiles);
   }
    @RequestMapping("selectByFileId/{fileId}")
    public SimpleCoreResponse selectByFileId1(@PathVariable Integer fileId)throws AdscmSystemException{
           Wrapper<SjFile> wrapper  =new EntityWrapper<>();
           wrapper.eq("fileId",fileId);
        List<SjFile> sjFiles = sjFileService.selectList(wrapper);
        return new SimpleCoreResponse("200","查询成功",sjFiles);
    }
}

